// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CMIInteractionData.java

package com.iphaeton.lms.course.scorm12.datamodel.cmi;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import com.iphaeton.lms.course.scorm12.datamodel.Element;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Vector;

// Referenced classes of package com.iphaeton.lms.course.scorm12.datamodel.cmi:
//            CMICategory, CMIRequest, DMErrorManager, CMIResponse

public class CMIInteractionData extends CMICategory
    implements Serializable
{

    public CMIInteractionData()
    {
        super(true);
        id = new Element("", "checkIdentifier", "NULL", true, false, false);
        time = new Element("", "checkTime", "NULL", true, false, false);
        type = new Element("", "checkVocabulary", "Interaction", true, false, false);
        weighting = new Element("", "checkDecimal", "NULL", true, false, false);
        student_response = new Element("", "checkFeedback", "NULL", true, false, false);
        result = new Element("", "checkVocabulary", "Result", true, false, false);
        latency = new Element("", "checkTimespan", "NULL", true, false, false);
        correct_responses = new Vector();
        objectives = new Vector();
        initialized = false;
    }

    public Element getID()
    {
        return id;
    }

    public Element getLatency()
    {
        return latency;
    }

    public Element getResult()
    {
        return result;
    }

    public Element getStudentResponse()
    {
        return student_response;
    }

    public Element getTime()
    {
        return time;
    }

    public Element getType()
    {
        return type;
    }

    public Element getWeighting()
    {
        return weighting;
    }

    public Vector getObjID()
    {
        return objectives;
    }

    public Vector getCorrectResponses()
    {
        return correct_responses;
    }

    public void setID(String inId)
    {
        id.setValue(inId);
        initialized = true;
    }

    public void setLatency(String inLatency)
    {
        latency.setValue(inLatency);
        initialized = true;
    }

    public void setResult(String inResult)
    {
        result.setValue(inResult);
        initialized = true;
    }

    public void setStudentResponse(String inStudentResponse)
    {
        student_response.setValue(inStudentResponse);
        initialized = true;
    }

    public void setTime(String inTime)
    {
        time.setValue(inTime);
        initialized = true;
    }

    public void setType(String inType)
    {
        type.setValue(inType);
        initialized = true;
    }

    public void setWeighting(String inWeighting)
    {
        weighting.setValue(inWeighting);
        initialized = true;
    }

    public void setObjID(String inObjId, int index)
    {
        objectives.add(index, inObjId);
    }

    public void setCorrectResponses(CMIResponse inResponse, int index)
    {
        correct_responses.add(index, inResponse);
    }

    public boolean isInitialized()
    {
        return initialized;
    }

    public void performSet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        String token = theRequest.getNextToken();
        if(theRequest.hasMoreTokensToProcess())
        {
            if(token.equals("objectives"))
            {
                token = theRequest.getNextToken();
                performSetForObjID(token, theRequest, dmErrorMgr);
            } else
            if(token.equals("correct_responses"))
            {
                token = theRequest.getNextToken();
                performSetForResponse(token, theRequest, dmErrorMgr);
            } else
            {
                if(_Debug)
                {
                    System.out.println("\t\tError - Data Model Element not implemented");
                    System.out.println("\t\tElement being processed: " + token + " is not a valid element of the CMI Interactions\n" + "Data Model Category");
                }
                dmErrorMgr.recNotImplementedError(theRequest);
            }
        } else
        if(theRequest.isAKeywordRequest())
        {
            dmErrorMgr.recKeyWordError(theRequest.getElement());
        } else
        {
            doSet(this, token, theRequest.getValue(), dmErrorMgr);
            initialized = true;
        }
    }

    private void performSetForObjID(String token, CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        int arrayIndex = getArrayIndex(token, theRequest, dmErrorMgr);
        String value = theRequest.getValue();
        try
        {
            if(_Debug)
            {
                System.out.println("\t\tChecking to see if an element already exists");
                System.out.println("\t\tat array position " + arrayIndex);
            }
            Element tmpElement = (Element)objectives.elementAt(arrayIndex);
            if(_Debug)
            {
                System.out.println("\t\tElement already exists");
                System.out.println("\t\tVector size: " + objectives.size());
            }
            boolean result = validateType(this, tmpElement, "id", value, dmErrorMgr);
            if(result)
            {
                tmpElement.setValue(value);
                objectives.set(arrayIndex, tmpElement);
            } else
            {
                if(_Debug)
                {
                    System.out.println("***** INVALID LMSSetValue() CALL *****");
                    System.out.println("          Invalid Type                ");
                    System.out.println("Element was NOT set!");
                    System.out.println("");
                    System.out.println("\t\tVector size: " + objectives.size());
                }
                dmErrorMgr.SetCurrentErrorCode("405");
            }
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            if(arrayIndex <= objectives.size())
            {
                Element tmpElement = new Element(theRequest.getValue(), "checkIdentifier", "NULL", true, false, false);
                boolean result = validateType(this, tmpElement, "id", theRequest.getValue(), dmErrorMgr);
                if(result)
                {
                    objectives.add(arrayIndex, tmpElement);
                } else
                {
                    if(_Debug)
                    {
                        System.out.println("***** INVALID LMSSetValue() CALL *****");
                        System.out.println("          Invalid Type                ");
                        System.out.println("Element was NOT set!");
                        System.out.println("");
                        System.out.println("\t\tVector size: " + objectives.size());
                    }
                    dmErrorMgr.SetCurrentErrorCode("405");
                }
            } else
            {
                dmErrorMgr.SetCurrentErrorCode("201");
            }
        }
    }

    private void performSetForResponse(String token, CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        int arrayIndex = getArrayIndex(token, theRequest, dmErrorMgr);
        String value = theRequest.getValue();
        try
        {
            if(_Debug)
            {
                System.out.println("\t\tChecking to see if an element already exists");
                System.out.println("\t\tat array position " + arrayIndex);
            }
            CMIResponse tmpResponse = (CMIResponse)correct_responses.elementAt(arrayIndex);
            if(_Debug)
            {
                System.out.println("\t\tElement already exists");
                System.out.println("\t\tSet the value in the element and replace");
                System.out.println("\t\tVector size: " + correct_responses.size());
            }
            tmpResponse.performSet(theRequest, dmErrorMgr);
            correct_responses.set(arrayIndex, tmpResponse);
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            if(_Debug)
            {
                System.out.println("\t\tElement does not already exists");
                System.out.println("\t\tVector size: " + correct_responses.size());
            }
            if(arrayIndex <= correct_responses.size())
            {
                CMIResponse response = new CMIResponse();
                response.performSet(theRequest, dmErrorMgr);
                correct_responses.add(arrayIndex, response);
            } else
            {
                dmErrorMgr.SetCurrentErrorCode("201");
            }
        }
    }

    private int getArrayIndex(String token, CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        int index = -1;
        try
        {
            Integer tmpInt = new Integer(token);
            index = tmpInt.intValue();
        }
        catch(NumberFormatException nfe)
        {
            if(_Debug)
            {
                System.out.println("\t\tError - Data Model Element not implemented");
                System.out.println("\t\tInvalid data model element: " + theRequest.getRequest());
                System.out.println("\t\tInvalid element " + token + ", Array index expected");
            }
            dmErrorMgr.SetCurrentErrorCode("401");
        }
        return index;
    }

    public String getCount(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        String result = new String("");
        String token = theRequest.getNextToken();
        if(token.equals("objectives"))
        {
            int count = 0;
            count = objectives.size();
            Integer tmpInt = new Integer(count);
            result = tmpInt.toString();
        } else
        if(token.equals("correct_responses"))
        {
            int count = 0;
            count = correct_responses.size();
            Integer tmpInt = new Integer(count);
            result = tmpInt.toString();
        } else
        {
            if(_Debug)
            {
                System.out.println("\t\tInvalid LMSGetValue() request: " + theRequest.getRequest());
                System.out.println("\t\tSCO is not permitted to call LMSGetValue() for cmi.interactions");
            }
            dmErrorMgr.SetCurrentErrorCode("404");
        }
        return result;
    }

    private static final long serialVersionUID = 0x56e051b69194aff5L;
    private static boolean _Debug;
    public Element id;
    public Vector objectives;
    public Element time;
    public Element type;
    public Vector correct_responses;
    public Element weighting;
    public Element student_response;
    public Element result;
    public Element latency;
    private boolean initialized;

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
